package com.ys.nms.module.business.service.ipv4Addresses;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.ipv4Addresses.vo.*;
import com.ys.nms.module.business.dal.dataobject.ipv4Addresses.Ipv4AddressesDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.ipv4Addresses.Ipv4AddressesMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * IP地址 Service 实现类
 *
 * @author zzi
 */
@Service
@Validated
public class Ipv4AddressesServiceImpl implements Ipv4AddressesService {

    @Resource
    private Ipv4AddressesMapper ipv4AddressesMapper;

    @Override
    public Integer createIpv4Addresses(Ipv4AddressesSaveReqVO createReqVO) {
        // 插入
        Ipv4AddressesDO ipv4Addresses = BeanUtils.toBean(createReqVO, Ipv4AddressesDO.class);
        ipv4AddressesMapper.insert(ipv4Addresses);

        // 返回
        return ipv4Addresses.getIpv4AddressId();
    }

    @Override
    public void updateIpv4Addresses(Ipv4AddressesSaveReqVO updateReqVO) {
        // 校验存在
        validateIpv4AddressesExists(updateReqVO.getIpv4AddressId());
        // 更新
        Ipv4AddressesDO updateObj = BeanUtils.toBean(updateReqVO, Ipv4AddressesDO.class);
        ipv4AddressesMapper.updateById(updateObj);
    }

    @Override
    public void deleteIpv4Addresses(Integer id) {
        // 校验存在
        validateIpv4AddressesExists(id);
        // 删除
        ipv4AddressesMapper.deleteById(id);
    }

    @Override
        public void deleteIpv4AddressesListByIds(List<Integer> ids) {
        // 删除
        ipv4AddressesMapper.deleteByIds(ids);
        }


    private void validateIpv4AddressesExists(Integer id) {
        if (ipv4AddressesMapper.selectById(id) == null) {
            throw exception(IPV4_ADDRESSES_NOT_EXISTS);
        }
    }

    @Override
    public Ipv4AddressesDO getIpv4Addresses(Integer id) {
        return ipv4AddressesMapper.selectById(id);
    }

    @Override
    public PageResult<Ipv4AddressesDO> getIpv4AddressesPage(Ipv4AddressesPageReqVO pageReqVO) {
        return ipv4AddressesMapper.selectPage(pageReqVO);
    }

}